<?php
function getFeatureList(){
    global $db;
    $sql = "SELECT title,title_link,thumb,description FROM property WHERE 1 AND status='normal' ORDER BY view_count DESC LIMIT 5";
    return $db->query($sql);
}

function getPropertySellLeft(){
    global $db;
    $sql = "SELECT p.*, g.geo_title, pc.cat_title
            FROM property p LEFT JOIN cat_property pc ON p.cat_id=pc.cat_id
            RIGHT JOIN geographic g ON p.geo_id=g.geo_id
            WHERE 1 AND p.status='actived' AND p.lang='".$_SESSION["lang"]."' ORDER BY p.order_num";
    $rs = $db->query($sql);
    return $rs;
}

function getPropertyTab($type='sell'){
    global $db;
    $sql = "SELECT title,title_link,thumb,description FROM property WHERE 1 AND status='normal' AND type='$type' ORDER BY view_count DESC LIMIT 5";
    return $db->query($sql);
}

function getNewsHome(){
    global $db;
    $sql = "SELECT title,title_link,image,description,create_date FROM news WHERE 1 AND cat_id <>4 AND  active='yes' AND lang='".$_SESSION["lang"]."' ORDER BY view_count DESC LIMIT 5";
    return $db->query($sql);
}
function getNewsReal(){
    global $db;
    $sql = "SELECT title,title_link,image,description,create_date FROM news WHERE cat_id=4 AND active='yes' AND lang='".$_SESSION["lang"]."' ORDER BY view_count DESC LIMIT 4";
    return $db->query($sql);
}

function getAllCity() {
    global $db;
    $sql="SELECT geo_id,geo_title FROM geographic WHERE geo_link IN ('Ha-Noi','TP-Ho-chi-minh','Da-Nang') AND (geo_parent=1 OR geo_parent=2 OR geo_parent=3)
          UNION
          SELECT p.* FROM (SELECT geo_id,geo_title FROM  geographic WHERE  geo_link NOT IN ('Ha-Noi','TP-Ho-chi-minh','Da-Nang') AND (geo_parent=1 OR geo_parent=2 OR geo_parent=3) ORDER BY  geo_title ASC) AS p";
    return $db->query($sql);
}

function getDistrictByCityId($cityId) {
    global $db;
    $sql="SELECT geo_id,geo_title FROM geographic WHERE geo_parent=".intval($cityId);
    return $db->query($sql);
}
function listPropertyCategoryByParent($parent_id = 0,$space = ' ', $trees = NULL) {
    global $db;
    if(!$trees) $trees = array();
    $rsCat = $db->query("SELECT * FROM cat_property WHERE  cat_parent = ".intval($parent_id)." ORDER BY cat_order,cat_id");
    while($rs=$db->fetch_array($rsCat)) {
        if($rs['cat_parent']==0) {
            $trees[] = array("cat_id"=>$rs['cat_id'],"cat_title"=>$rs['cat_title'],"cat_active"=>$rs['cat_active'],"create_date"=>$rs['create_date']);
        }else {
            $trees[] = array("cat_id"=>$rs['cat_id'],"cat_title"=>$space.$rs['cat_title'],"cat_active"=>$rs['cat_active'],"create_date"=>$rs['create_date']);
        }
    }
    return $trees;
}

function getProjectCatHome(){
    global $db;
    $sql = "SELECT * FROM cat_projects WHERE 1 AND cat_active='yes' AND lang='".$_SESSION["lang"]."' ORDER BY cat_order limit 3";
    return $db->query($sql);
}

function getProjectByCatIdHome($catId){
    global $db;
    $sql = "SELECT * FROM projects WHERE 1 AND cat_id='$catId' ORDER BY create_date DESC LIMIT 1";
    return $db->query($sql);
}

function getFAQHome(){
    global $db;
    $sql = "SELECT faq.*,cat_faq.cat_name FROM faq,cat_faq WHERE 1 AND faq.lang='".$_SESSION["lang"]."' AND faq.cat_id = cat_faq.cat_id ORDER BY id DESC LIMIT 3";
    return $db->query($sql);
}

function getCatNewsToArray($parent_id = 0,$trees=null) {
    global $db;
    if(!$trees) $trees = array();
    $rsCat = $db->query("SELECT * FROM cat_news WHERE  cat_parent = ".intval($parent_id)." AND lang='".$_SESSION["lang"]."' LIMIT 4");
    if($rsCat) {
        while($rs = $db->fetch_array($rsCat)) {
            $trees[] = array("cat_id"=>$rs['cat_id'],"cat_title"=>$rs['cat_title'],"cat_link"=>$rs['cat_link']);
            $trees = getCatNewsToArray($rs['cat_id'],$trees);
        }
    }
    return $trees;
}

function getNewestVideo(){
    global $db;
    $sql = "SELECT * FROM video WHERE 1 ORDER BY id DESC";
    return $db->query_first($sql);
}

?>